package com.qizer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.qizer.bean.User;
import com.qizer.net.ApacheHttpConnect;
import com.qizer.net.NetParameter;
import com.qizer.util.Utils;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
/**
 * 
 * */
public class ComsuterCountResultDetailActivity extends Activity {
	TextView cqc_result_detail_product;
	TextView cqc_result_detail_querytime;
	ListView cqc_result_detail_list;
	Button cqc_result_detail_back;
	List<Map<String, Object>> datalist;
	String time;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comsuterquerycount_result_detail);
		cqc_result_detail_product = (TextView) findViewById(R.id.cqc_result_detail_product);
		String product = (String) ComsuterCountResultDetailActivity.this
				.getIntent().getExtras().getString("product");
		cqc_result_detail_product.setText(product);	
		cqc_result_detail_querytime = (TextView) findViewById(R.id.cqc_result_detail_querytime);
		time = (String) ComsuterCountResultDetailActivity.this.getIntent()
				.getExtras().getString("time");
		cqc_result_detail_querytime.setText(time);
		cqc_result_detail_list = (ListView) findViewById(R.id.cqc_result_detail_list);
		SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.comusterquerycount_result_detail_items,
                new String[]{"QueryTIme","Location","BarCode"},
                new int[]{R.id.cqc_list_detail_date,R.id.cqc_list_detail_location,R.id.cqc_list_detail_code});

		cqc_result_detail_list.setAdapter(adapter);

		cqc_result_detail_back = (Button) findViewById(R.id.cqc_result_detail_back);
		cqc_result_detail_back.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				SimpleDateFormat faFormat = new SimpleDateFormat("yyyy-MM");
				SimpleDateFormat faFormat2 = new SimpleDateFormat("yyyy-MM-dd");
				ComsuterQueryCount.cqc.cqc_product.setText("");
				ComsuterQueryCount.cqc.cqc_start.setText(faFormat.format(new Date())+"-01");
				ComsuterQueryCount.cqc.cqc_end.setText(faFormat2.format(new Date()));
				finish();
			}
		});
	}

	public List<Map<String, Object>> getData() {
		datalist = new ArrayList<Map<String, Object>>();
		String url = NetParameter.url_comsuterQueryCountDetail + "CommodityNo="
				+(String) ComsuterCountResultDetailActivity.this
				.getIntent().getExtras().getString("productId")+ "&time="
				+ time;
		Log.i("ComsuterCountResultDetailActivity", "ComsuterCountResultDetailActivity:"+url);
		try {
			String result = ApacheHttpConnect.requestByHttpGet(url);
			JSONArray jsons = Utils.getJSONArray(result);
			Log.i("QueryOutStock", "jsons=" + jsons.toString());
			for (int i = 0; i < jsons.length(); i++) {
				JSONObject jo = jsons.getJSONObject(i);
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("QueryTIme", jo.get("QueryTIme").toString());
				map.put("Location", jo.get("Location").toString());
				map.put("BarCode", jo.get("BarCode").toString());
				datalist.add(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return datalist;

	}
}